package cn.client.ui.view

import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import cn.client.data.remoteData.bean.PageSelectByUid
import cn.client.data.userStateManager
import cn.client.navigation.destinations.AppStart
import cn.client.navigation.destinations.Overview
import cn.client.navigation.destinations.StartLoading
import cn.client.ui.view.main.dynamic.DynamicViewModel
import cn.client.ui.view.main.user.UserInfoViewModel
import kotlinx.coroutines.flow.toCollection


@Composable
fun StartScreen(
    navController: NavController,
    viewModel: UserInfoViewModel = hiltViewModel()
) {
    val context = LocalContext.current
    // 观察加载状态
    val isLoading = viewModel.isLoading.collectAsState(initial = false)

    // 使用 collectAsState 观察 userId 的变化
//    val userIdState = viewModel.userId.collectAsState(initial = -1)
//    val userId = userIdState.value
    val userIdState = userStateManager.userId.collectAsState(initial = -1)
    val userId = userIdState.value

    if (userId == -1){
        viewModel.dispatch(UserInfoViewModel.PageIntent.GetUser("获取本地用户缓存"))
    } else {
        navController.navigate(Overview.route) {
            popUpTo(StartLoading.route) {
                inclusive = true
            }
            launchSingleTop = true
        }
    }

    LaunchedEffect(Unit) {
        viewModel.pageEvents.collect {
            when (it) {
                is UserInfoViewModel.PageEvent.UserRes -> {
                    // 跳转页面
                    Toast.makeText(context, "提示：查询成功", Toast.LENGTH_SHORT).show()
                    navController.navigate(Overview.route) {
                        popUpTo(StartLoading.route) {
                            inclusive = true
                        }
                        launchSingleTop = true
                    }
                }

                is UserInfoViewModel.PageEvent.GetErr -> {
                    Toast.makeText(context, "提示：${it.msg}", Toast.LENGTH_SHORT).show()
                    navController.navigate(AppStart.route) {
                        popUpTo(StartLoading.route) {
                            inclusive = true
                        }
                        launchSingleTop = true
                    }
                }

                else -> {}
            }
        }
    }

    Box(
        contentAlignment = Alignment.Center,
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)
    ) {
        if (isLoading.value) {
            // 显示加载动画
            CircularProgressIndicator(modifier = Modifier.size(50.dp))
        }
    }


}